package com.vm.osm.business.service.eam.impl;

import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.eam.equ.*;
import com.vm.osm.business.service.eam.IEamEquService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 设备服务实现类
 *
 * @author Xuerong Xue
 */
@Service
public class EamEquServiceImpl implements IEamEquService {
    private static final Logger LOG = LoggerFactory.getLogger(EamEquServiceImpl.class);
    @Value("${eam_api_url}")
    private String EAM_API_URL;
    @Autowired
    private RestTemplate restTemplate;


    @Override
    public List<EquCommonAttr> getCommonAttr(GetCommonAttrReq request) {
        try {
            LOG.info("【设备】（获取通用属性）请求参数={}", request);
            if (null == request) return Collections.emptyList();
            String URL = EAM_API_URL + "/eam/v1.0/equipments/common_attrs";
            if (null != request.getEqu_type()) {
                URL += "?industry_type=" + request.getEqu_type();
                if (StringUtils.isNotEmpty(request.getEqu_type_id())) {
                    URL += "&industry_id=" + request.getEqu_type_id();
                }
            }
            HttpHeaders headers = new HttpHeaders();
            headers.add("session_id", request.getSession_id());
            HttpEntity<Object> entity = new HttpEntity<>(null, headers);
            ResponseEntity<EquCommonAttr[]> exchange =
                    restTemplate.exchange(URL, HttpMethod.GET, entity, EquCommonAttr[].class);
            return Arrays.asList(exchange.getBody());
        } catch (HttpClientErrorException e) {
            LOG.error("【设备】（获取通用属性）异常", e.getResponseBodyAsString());
            return null;
        } catch (Exception e) {
            LOG.error("【设备】（获取通用属性）异常", e.getMessage());
            return null;
        }
    }

    @Override
    public List<EquDiyAttr> getDiyAttr(GetDiyAttrReq request) {
        try {
            LOG.info("【设备】（获取自定义属性）请求参数={}", request);
            if (null == request) return Collections.emptyList();
            String URL = EAM_API_URL + "/eam/v1.0/equipments/diy_attrs" +
                    "?industry_type=" + request.getEqu_type() + "&equ_type_id=" + request.getEqu_type_id();
            HttpHeaders headers = new HttpHeaders();
            headers.add("session_id", request.getSession_id());
            HttpEntity<Object> entity = new HttpEntity<>(null, headers);
            ResponseEntity<EquDiyAttr[]> exchange =
                    restTemplate.exchange(URL, HttpMethod.GET, entity, EquDiyAttr[].class);
            return Arrays.asList(exchange.getBody());
        } catch (HttpClientErrorException e) {
            LOG.error("【设备】（获取自定义属性）异常", e.getResponseBodyAsString());
            return null;
        } catch (Exception e) {
            LOG.error("【设备】（获取自定义属性）异常", e.getMessage());
            return null;
        }
    }

    @Override
    public SearchEamEquRsp search(SearchEamEquReq request) {
        try {
            LOG.info("【设备】（查询设备信息）请求参数={}", request);
            if (null == request) throw new BusinessException("请求参数为空");
            if (StringUtils.isEmpty(request.getEnterprise_id())) throw new BusinessException("企业Id为空");
            String URL = EAM_API_URL + "/eam/v1.0/" + request.getEnterprise_id() + "/equipments/search";
            HttpHeaders headers = new HttpHeaders();
            headers.add("session_id", request.getSession_id());
            HttpEntity<SearchEamEquReq> entity = new HttpEntity<>(request, headers);
            ResponseEntity<SearchEamEquRsp> exchange =
                    restTemplate.exchange(URL, HttpMethod.POST, entity, SearchEamEquRsp.class);
            return exchange.getBody();
        } catch (HttpClientErrorException e) {
            LOG.error("【设备】（查询设备信息）异常", e.getResponseBodyAsString());
            return null;
        } catch (Exception e) {
            LOG.error("【设备】（查询设备信息）异常", e.getMessage());
            return null;
        }
    }

    @Override
    public EamEqu get(GetEamEquReq request) {
        try {
            LOG.info("【设备】（根据Id获取设备信息）请求参数={}", request);
            if (null == request) {
                LOG.warn("【设备】（根据Id获取设备信息）请求参数为空");
                return null;
            }
            String URL = EAM_API_URL + "/eam/v1.0/equipments/" + request.getId();
            HttpHeaders headers = new HttpHeaders();
            headers.add("session_id", request.getSession_id());
            HttpEntity<Object> entity = new HttpEntity<>(null, headers);
            ResponseEntity<EamEqu> exchange =
                    restTemplate.exchange(URL, HttpMethod.GET, entity, EamEqu.class);
            return exchange.getBody();
        } catch (HttpClientErrorException e) {
            LOG.error("【设备】（根据Id获取设备信息）异常", e.getResponseBodyAsString());
            return null;
        } catch (Exception e) {
            LOG.error("【设备】（根据Id获取设备信息）异常", e.getMessage());
            return null;
        }
    }
}
